﻿@model PaymentInfoModel
@using Logistics.Web.Models.Order
<script type="text/javascript">
    var pagename = "LogisticsPayment";
    var creditCard = {
        PaySuccessRe: function () {//获取已有信用卡的信息列表，如果有则显示，如果没有则不显示
            var that = this;
            $(".logistics-order-loading").show();
            $.ajax({
                cache: false,
                type: "POST",
                url: '@Url.Action("GetPaymentsCreditCard", "Order")',
                success: function (data) {
                    if (data != null && data.name === "GetPaymentList") {
                        $("#creditCardList").html(data.html);
                        that.ClickContinue();
                    }
                    $(".logistics-order-loading").hide();
                },
                error: function (xhr, ajaxOptions, thrownError) {
                    showNotification('Failed to update the system.');
                    $(".logistics-order-loading").hide();
                }
            });
        },
        ClickContinue: function () {
            $("input[Value='Select']").click(function () {
                var creditCardId = $(this).attr("data-card");
                $("#CreditCard").val(creditCardId);
                $("#Continue").click();
            });
        },
        Init: function () {
            this.PaySuccessRe();
        }
    };
    $(document).ready(function () {
        if ($("#creditCardList").length) {
            creditCard.Init();
        }
        $("button").live("click", function (e) {
            e.stopPropagation();
        });
    });
    function loadCreditCardForm(url, data) {
        $('.payment-form-load').empty();
        $(".logistics-order-loading").show();
        $("#payment-info-form").hide();
        data['type'] = "logistics";
        $.ajax({
            cache: false,
            type: "POST",
            url: url,
            data: data,
            success: function (data) {
                $(".logistics-order-loading").hide();
                if (data.success) {
                    $('#form-additional').html(data);
                    $('.payment-form-load').html('<h3>Add Credit Card</h3><iframe class="payment-globalpayments-secureForm" src="' + data.url + '"></iframe>');
                    uid = data.uid;
                }
                else {
                    $("#payment-info-form").show();
                    showNotification(data.errors);
                }
            },
            error: function (xhr, ajaxOptions, thrownError) {
                $(".logistics-order-loading").hide();
                showNotification('Failed to update the system.');
            }
        });
    }
    function removeCreditCard(url, data) {
        $.ajax({
            cache: false,
            type: "POST",
            url: url,
            data: data,
            success: function (data) {
                if (data.success) {
                    showNotification(data.errors);
                }
                else {
                    showNotification(data.errors);
                }
            },
            error: function (xhr, ajaxOptions, thrownError) {
                showNotification('Failed to update the system.');
            }
        });
    }
    function processCardOnFile(param) {
        submitPageRequest($('<input data-name="checkNewCard" data-value="@Boolean.TrueString">'), $("#payment-info-form"));
    }
    function showCreditCardConfirm() {
        $(".payment-action-button").append('<input type="button" class="btn btn-normal form-control" data-action="submit" value="Continue" />');
    }
    function showCVV() {
        $('#show-cvv').modal({

        });
    }
    function handlePaymentFormError(errorCode, gatewayCode, gatewayMessage) {
        $(".alert").remove();
        $('#creditCardForm').fadeOut(200, function () {
            $('#creditCardList, .payment-continue-section').fadeIn(200);
        });
        var message = errorCode + " ";
        if (gatewayMessage != null) {
            message = message + gatewayMessage;
        }
        $(".logistics-order-messages").prepend('<div class="alert alert-danger alert-dismissible" role="alert"><button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></button><span>Error ' + message + '</span></div>');
        console.log("Error Code:" + errorCode);
    }
</script>
<div id="show-cvv" class="modal fade" tabindex="-1" role="dialog">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                <h4 class="modal-title" id="myModalLabel">What is CVC?</h4>
            </div>
            <div class="modal-body">
                <p>The CVC/CVV Number on your credit card or debit card is a 3 digit number on VISA or MasterCard branded credit cards. </p>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
            </div>
        </div>
    </div>
</div>
<div class="payment-form-load">
</div>
@using (Html.BeginForm("PaymentInfo", "Order", FormMethod.Post, new { id = "payment-info-form" }))
{
    @Html.HiddenFor(model => model.StoreId)
    @Html.HiddenFor(model => model.CustomerId)
    <div class="section-wrapper">
        <div class="col-sm-12">
            <h3>Payment Information</h3>
            <div class="payment-info">
                @if (Model.IsDebug)
                {
                    <input type="button" class="btn btn-primary" data-action="submit" value="Continue Saving Test Payment Info" />
                }
                else if (Model.PaymentRequired)
                {
                    <div id="creditCardList">
                    </div>
                    <div id="creditCardForm">
                    </div>
                    <div class="clear"></div>
                    <input type="button" class="btn btn-primary " style="display: none;" id="Continue" data-action="submit" value="Continue" />
                }
                else
                {
                    <p>Payment is not required.</p>
                }
            </div>
        </div>
        <div class="col-sm-12">
            <h3>Billing Address</h3>
            <div class="billing-address-container">
                @if (Model.BillingAddress != null && Model.BillingAddress.Id > 0)
                {
                    if (!String.IsNullOrEmpty(Model.BillingAddress.Company))
                    {
                        <div class="billing-address-company">
                            <strong>
                                @Html.DisplayFor(model => model.BillingAddress.Company)
                            </strong>
                        </div>
                    }
                    else
                    {
                        <div class="billing-address-name">
                            <strong>
                                @Html.DisplayFor(model => model.BillingAddress.FirstName)
                                @Html.DisplayFor(model => model.BillingAddress.LastName)
                            </strong>
                        </div>
                    }
                    <div class="billing-address-1">
                        @Html.DisplayFor(model => model.BillingAddress.Address1)
                    </div>
                    <div class="billing-address-2">
                        @Html.DisplayFor(model => model.BillingAddress.Address2)
                    </div>
                    <div class="billing-city-province-zip">
                        @Html.DisplayFor(model => model.BillingAddress.City),
                        @Html.DisplayFor(model => model.BillingAddress.StateProvinceName),
                        @Html.DisplayFor(model => model.BillingAddress.ZipPostalCode)
                    </div>
                    <div class="billing-phone-number">
                        @if (!String.IsNullOrWhiteSpace(Model.BillingAddress.CellPhoneNumber))
                        {
                            @Html.DisplayFor(model => model.BillingAddress.CellPhoneNumber)
                        }
                        @if (!String.IsNullOrWhiteSpace(Model.BillingAddress.PhoneNumber))
                        {
                            @Html.DisplayFor(model => model.BillingAddress.PhoneNumber)
                        }
                        @if (!String.IsNullOrWhiteSpace(Model.BillingAddress.Email))
                        {
                            <div class="billing-email-address">
                                @Html.DisplayFor(model => model.BillingAddress.Email)
                            </div>
                        }
                    </div>
                }
            </div>
            <div>
                <input type="button" class="address-select btn btn-normal form-control" data-action="submit" data-name="changeBillingAddress" data-value="@Boolean.TrueString" value="Change" />
            </div>
        </div>
        <div class="clear"></div>
    </div>
    <div class="payment-action-button col-sm-12 col-lg-6 col-lg-offset-3"></div>
}
